package com.cstz.hb.manage.service.impl;

import com.cstz.hb.manage.dao.AppUserDao;
import com.cstz.hb.manage.dao.SysDeptDao;
import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.SysDept;
import com.cstz.hb.manage.service.AppUserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AppUserServiceImpl implements AppUserService {
	@Autowired
	private AppUserDao appUserDao;

	@Autowired
    private SysDeptDao sysDeptDao;
	
	@Override
	public AppUser get(Long id){
		return appUserDao.get(id);
	}
	
	public AppUser getByMobile(String mobile){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", mobile);
		
		List<AppUser> list = this.list(map);
		
		if(CollectionUtils.isNotEmpty(list)){
			return list.get(0);
		}
		
		return null;
	}
	
	@Override
	public List<AppUser> list(Map<String, Object> map){
		List<AppUser> list = appUserDao.list(map);
		list.forEach(e->{
            SysDept sysDept = sysDeptDao.getById(e.getDeptId());
		    e.setDeptName(null!=sysDept?sysDept.getName():"");
        });
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return appUserDao.count(map);
	}
	
	@Override
	public int save(AppUser appUser){
		return appUserDao.save(appUser);
	}
	
	@Override
	public int update(AppUser appUser){
		return appUserDao.update(appUser);
	}
	
	@Override
	public int remove(Long id){
		return appUserDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return appUserDao.batchRemove(ids);
	}

}
